/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package db.genomemap;

import db.gui.RDBConnection;

import genomemap.data.GDataSource;
import genomemap.data.Gene;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashSet;

/**
 * @author Susanta Tewari
 * @version 1.0
 * @created 08-Nov-2007 8:43:54 PM
 */
public class G_DBDataSource extends GDataSource {

    /**
     * Constructs ...
     */
    public G_DBDataSource() {}

    /**
     * Constructs ...
     *
     * @param ch_id description
     * @param order description
     */
    public G_DBDataSource(int ch_id, int[] order) {

        setChId(ch_id);
        setOrder(order);
    }

    public void loadData() {

        int[][] rflpData = null;

        try {

            RDBConnection dbConnection = new RDBConnection();
            Statement stmt             = dbConnection.getStatement();
            ResultSet rs               = null;
            String query               = null;

            rflpData = null;

            int cursorIndex = 0;

            /**
             *  GET THE ROWS
             */
            query = "SELECT distinct r.rowId from rflp r " + "where r.ch_id = " + getChId() + " ;";
            rs    = stmt.executeQuery(query);

            rs.last();

            int[] rowArray = new int[rs.getRow()];

            rs.beforeFirst();

            cursorIndex = 0;

            while (rs.next()) {

                rowArray[cursorIndex] = rs.getInt("rowId");

                cursorIndex++;
            }

            /**
             *  GET THE COLUMNS
             */
            query = "SELECT distinct r.progenyId from rflp r " + "where r.ch_id = " + getChId()
                    + " ;";
            rs = stmt.executeQuery(query);

            rs.last();

            int[] colArray = new int[rs.getRow()];

            rs.beforeFirst();

            cursorIndex = 0;

            while (rs.next()) {

                colArray[cursorIndex] = rs.getInt("progenyId");

                cursorIndex++;
            }

            /**
             *  INITIALIZE THE rflpData
             */
            rflpData = new int[rowArray.length][colArray.length];

            /**
             *  FILL UP  rflpData
             */
            query = "SELECT r.value from rflp r " + "where r.ch_id = " + getChId();

            for (int rowIndex = 0; rowIndex < rflpData.length; rowIndex++) {

                for (int colIndex = 0; colIndex < rflpData[0].length; colIndex++) {

                    String loopQuery = query + " && r.rowId = " + rowArray[rowIndex]
                                       + " && r.progenyId = " + colArray[colIndex];

                    rs = stmt.executeQuery(loopQuery);

                    rs.next();

                    rflpData[rowIndex][colIndex] = rs.getInt("value");
                }
            }

            dbConnection.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        setRflpData(Imputation(rflpData));
    }

    /**
     * requires only id to be set; no order
     */
    public LinkedHashSet<Gene> getFullGeneSet() {

        RDBConnection dbConnection = new RDBConnection();
        Statement stmt             = dbConnection.getStatement();

        if (stmt == null) return null;

        ResultSet rs                = null;
        String query                = null;
        int ch_id                   = getChId();
        LinkedHashSet<Gene> geneSet = new LinkedHashSet<>();

        query = " SELECT g.gene_name,g.start,g.end,g.gene_order FROM genes g "
                + " WHERE g.ch_id = " + ch_id + " ;";

        try {

            rs = stmt.executeQuery(query);

            while (rs.next()) {

                Gene gene = new Gene();

                gene.setLabel(rs.getString("gene_name"));
                gene.setStartPos(rs.getInt("start"));
                gene.setEndPos(rs.getInt("end"));
                gene.setOrder(rs.getInt("gene_order"));
                geneSet.add(gene);
            }

            dbConnection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return geneSet;
    }
}
